package com.ffs.game.scene.obj;

import com.ffs.persist.utils.TimeUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 仇恨管理
 *
 * @author ffs80
 */
public class ThreatManager {

    private static final ThreatManager INSTANCE = new ThreatManager();

    /**
     * 仇恨map
     * key:仇恨对象
     * key:敌对对象
     * value：具体仇恨值
     */
    private final Map<Long, Map<Long, Integer>> threatMap = new ConcurrentHashMap<>();

    /**
     * 仇恨更新时间
     * key:仇恨对象
     * value:更新时间
     */
    private final Map<Long, Long> threatTime = new ConcurrentHashMap<>();


    public static ThreatManager getInstance() {
        return INSTANCE;
    }

    /**
     * 获取仇恨信息
     */
    public Map<Long, Integer> findThreatMap(long hatredId) {
        Map<Long, Integer> threat = threatMap.get(hatredId);
        if (threat == null) {
            threat = new ConcurrentHashMap<>();
            threatMap.put(hatredId, threat);
        }
        return threat;
    }

    /**
     * 添加敌人仇恨值
     *
     * @param hater   仇恨对象
     * @param enemyId 敌人ud
     * @param value   仇恨值
     * @param isTaunt 是否嘲讽
     */
    public void addEnemyThreat(MapObject hater, long enemyId, long value, boolean isTaunt) {
        if (!isTaunt) {
            return;
        }

        long hartId = hater.getId();
        if (hartId != enemyId) {
            Map<Long, Integer> threat = findThreatMap(hartId);
            threat.merge(enemyId, (int) value, (a, b) -> ((a + b) < 0 ? a : a + b));
            long nowMils = TimeUtil.getNowMils();
            threatTime.put(hartId, nowMils);
        }
    }

    /**
     * 添加仇恨时间
     */
    public void addThreatTime(MapObject hater, long enemyId) {
        long haterId = hater.getId();
        if (haterId != enemyId) {
            long nowMils = TimeUtil.getNowMils();
            threatTime.put(haterId, nowMils);
        }
    }

    /**
     * 减少对目标的仇恨
     */
    public void reduceEnemyThreat(MapObject hater, long enemyId, long value) {
        long haterId = hater.getId();
        if (haterId != enemyId) {
            Map<Long, Integer> threat = findThreatMap(haterId);
            threat.merge(enemyId, (int) value, (a, b) -> (Math.max((a - b), 0)));
        }
    }

    /**
     * 清除与我敌对人的仇恨
     */
    public void removeEnemyThreat(MapObject hater) {

    }

    /**
     * 清除对象所有仇恨
     */
    public void clear(MapObject hater) {

    }


}
